<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>datetimeparse.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_i18n_datetimeparse.js.html">datetimeparse.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Date/Time parsing library with locale support.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>
<a name="line20"></a>/**
<a name="line21"></a> * Namespace for locale date/time parsing functions
<a name="line22"></a> */
<a name="line23"></a>goog.provide(&#39;goog.i18n.DateTimeParse&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.date&#39;);
<a name="line26"></a>goog.require(&#39;goog.i18n.DateTimeFormat&#39;);
<a name="line27"></a>goog.require(&#39;goog.i18n.DateTimeSymbols&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * DateTimeParse is for parsing date in a locale-sensitive manner. It allows
<a name="line32"></a> * user to use any customized patterns to parse date-time string under certain
<a name="line33"></a> * locale. Things varies across locales like month name, weekname, field
<a name="line34"></a> * order, etc.
<a name="line35"></a> *
<a name="line36"></a> * This module is the counter-part of DateTimeFormat. They use the same
<a name="line37"></a> * date/time pattern specification, which is borrowed from ICU/JDK.
<a name="line38"></a> *
<a name="line39"></a> * This implementation could parse partial date/time.
<a name="line40"></a> *
<a name="line41"></a> * Time Format Syntax: To specify the time format use a time pattern string.
<a name="line42"></a> * In this pattern, following letters are reserved as pattern letters, which
<a name="line43"></a> * are defined as the following:
<a name="line44"></a> *
<a name="line45"></a> * &lt;pre&gt;
<a name="line46"></a> * Symbol   Meaning                 Presentation        Example
<a name="line47"></a> * ------   -------                 ------------        -------
<a name="line48"></a> * G        era designator          (Text)              AD
<a name="line49"></a> * y#       year                    (Number)            1996
<a name="line50"></a> * M        month in year           (Text &amp; Number)     July &amp; 07
<a name="line51"></a> * d        day in month            (Number)            10
<a name="line52"></a> * h        hour in am/pm (1~12)    (Number)            12
<a name="line53"></a> * H        hour in day (0~23)      (Number)            0
<a name="line54"></a> * m        minute in hour          (Number)            30
<a name="line55"></a> * s        second in minute        (Number)            55
<a name="line56"></a> * S        fractional second       (Number)            978
<a name="line57"></a> * E        day of week             (Text)              Tuesday
<a name="line58"></a> * D        day in year             (Number)            189
<a name="line59"></a> * a        am/pm marker            (Text)              PM
<a name="line60"></a> * k        hour in day (1~24)      (Number)            24
<a name="line61"></a> * K        hour in am/pm (0~11)    (Number)            0
<a name="line62"></a> * z        time zone               (Text)              Pacific Standard Time
<a name="line63"></a> * Z        time zone (RFC 822)     (Number)            -0800
<a name="line64"></a> * v        time zone (generic)     (Text)              Pacific Time
<a name="line65"></a> * &#39;        escape for text         (Delimiter)         &#39;Date=&#39;
<a name="line66"></a> * &#39;&#39;       single quote            (Literal)           &#39;o&#39;&#39;clock&#39;
<a name="line67"></a> * &lt;/pre&gt;
<a name="line68"></a> *
<a name="line69"></a> * The count of pattern letters determine the format. &lt;p&gt;
<a name="line70"></a> * (Text): 4 or more pattern letters--use full form,
<a name="line71"></a> *         less than 4--use short or abbreviated form if one exists.
<a name="line72"></a> *         In parsing, we will always try long format, then short. &lt;p&gt;
<a name="line73"></a> * (Number): the minimum number of digits. &lt;p&gt;
<a name="line74"></a> * (Text &amp; Number): 3 or over, use text, otherwise use number. &lt;p&gt;
<a name="line75"></a> * Any characters that not in the pattern will be treated as quoted text. For
<a name="line76"></a> * instance, characters like &#39;:&#39;, &#39;.&#39;, &#39; &#39;, &#39;#&#39; and &#39;@&#39; will appear in the
<a name="line77"></a> * resulting time text even they are not embraced within single quotes. In our
<a name="line78"></a> * current pattern usage, we didn&#39;t use up all letters. But those unused
<a name="line79"></a> * letters are strongly discouraged to be used as quoted text without quote.
<a name="line80"></a> * That&#39;s because we may use other letter for pattern in future. &lt;p&gt;
<a name="line81"></a> *
<a name="line82"></a> * Examples Using the US Locale:
<a name="line83"></a> *
<a name="line84"></a> * Format Pattern                         Result
<a name="line85"></a> * --------------                         -------
<a name="line86"></a> * &quot;yyyy.MM.dd G &#39;at&#39; HH:mm:ss vvvv&quot; -&gt;&gt;  1996.07.10 AD at 15:08:56 Pacific Time
<a name="line87"></a> * &quot;EEE, MMM d, &#39;&#39;yy&quot;                -&gt;&gt;  Wed, July 10, &#39;96
<a name="line88"></a> * &quot;h:mm a&quot;                          -&gt;&gt;  12:08 PM
<a name="line89"></a> * &quot;hh &#39;o&#39;&#39;clock&#39; a, zzzz&quot;           -&gt;&gt;  12 o&#39;clock PM, Pacific Daylight Time
<a name="line90"></a> * &quot;K:mm a, vvv&quot;                     -&gt;&gt;  0:00 PM, PT
<a name="line91"></a> * &quot;yyyyy.MMMMM.dd GGG hh:mm aaa&quot;    -&gt;&gt;  01996.July.10 AD 12:08 PM
<a name="line92"></a> *
<a name="line93"></a> * &lt;p&gt; When parsing a date string using the abbreviated year pattern (&quot;yy&quot;),
<a name="line94"></a> * DateTimeParse must interpret the abbreviated year relative to some
<a name="line95"></a> * century. It does this by adjusting dates to be within 80 years before and 20
<a name="line96"></a> * years after the time the parse function is called. For example, using a
<a name="line97"></a> * pattern of &quot;MM/dd/yy&quot; and a DateTimeParse instance created on Jan 1, 1997,
<a name="line98"></a> * the string &quot;01/11/12&quot; would be interpreted as Jan 11, 2012 while the string
<a name="line99"></a> * &quot;05/04/64&quot; would be interpreted as May 4, 1964. During parsing, only
<a name="line100"></a> * strings consisting of exactly two digits, as defined by {@link
<a name="line101"></a> * java.lang.Character#isDigit(char)}, will be parsed into the default
<a name="line102"></a> * century. Any other numeric string, such as a one digit string, a three or
<a name="line103"></a> * more digit string will be interpreted as its face value.
<a name="line104"></a> *
<a name="line105"></a> * &lt;p&gt; If the year pattern does not have exactly two &#39;y&#39; characters, the year is
<a name="line106"></a> * interpreted literally, regardless of the number of digits. So using the
<a name="line107"></a> * pattern &quot;MM/dd/yyyy&quot;, &quot;01/11/12&quot; parses to Jan 11, 12 A.D.
<a name="line108"></a> *
<a name="line109"></a> * &lt;p&gt; When numeric fields abut one another directly, with no intervening
<a name="line110"></a> * delimiter characters, they constitute a run of abutting numeric fields. Such
<a name="line111"></a> * runs are parsed specially. For example, the format &quot;HHmmss&quot; parses the input
<a name="line112"></a> * text &quot;123456&quot; to 12:34:56, parses the input text &quot;12345&quot; to 1:23:45, and
<a name="line113"></a> * fails to parse &quot;1234&quot;. In other words, the leftmost field of the run is
<a name="line114"></a> * flexible, while the others keep a fixed width. If the parse fails anywhere in
<a name="line115"></a> * the run, then the leftmost field is shortened by one character, and the
<a name="line116"></a> * entire run is parsed again. This is repeated until either the parse succeeds
<a name="line117"></a> * or the leftmost field is one character in length. If the parse still fails at
<a name="line118"></a> * that point, the parse of the run fails.
<a name="line119"></a> *
<a name="line120"></a> * &lt;p&gt; Now timezone parsing only support GMT:hhmm, GMT:+hhmm, GMT:-hhmm
<a name="line121"></a> */
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * Construct a DateTimeParse based on current locale.
<a name="line127"></a> * @param {string|number} pattern pattern specification or pattern type.
<a name="line128"></a> * @constructor
<a name="line129"></a> * @final
<a name="line130"></a> */
<a name="line131"></a>goog.i18n.DateTimeParse = function(pattern) {
<a name="line132"></a>  this.patternParts_ = [];
<a name="line133"></a>  if (typeof pattern == &#39;number&#39;) {
<a name="line134"></a>    this.applyStandardPattern_(pattern);
<a name="line135"></a>  } else {
<a name="line136"></a>    this.applyPattern_(pattern);
<a name="line137"></a>  }
<a name="line138"></a>};
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Number of years prior to now that the century used to
<a name="line143"></a> * disambiguate two digit years will begin
<a name="line144"></a> *
<a name="line145"></a> * @type {number}
<a name="line146"></a> */
<a name="line147"></a>goog.i18n.DateTimeParse.ambiguousYearCenturyStart = 80;
<a name="line148"></a>
<a name="line149"></a>
<a name="line150"></a>/**
<a name="line151"></a> * Apply a pattern to this Parser. The pattern string will be parsed and saved
<a name="line152"></a> * in &quot;compiled&quot; form.
<a name="line153"></a> * Note: this method is somewhat similar to the pattern parsing method in
<a name="line154"></a> *       datetimeformat. If you see something wrong here, you might want
<a name="line155"></a> *       to check the other.
<a name="line156"></a> * @param {string} pattern It describes the format of date string that need to
<a name="line157"></a> *     be parsed.
<a name="line158"></a> * @private
<a name="line159"></a> */
<a name="line160"></a>goog.i18n.DateTimeParse.prototype.applyPattern_ = function(pattern) {
<a name="line161"></a>  var inQuote = false;
<a name="line162"></a>  var buf = &#39;&#39;;
<a name="line163"></a>
<a name="line164"></a>  for (var i = 0; i &lt; pattern.length; i++) {
<a name="line165"></a>    var ch = pattern.charAt(i);
<a name="line166"></a>
<a name="line167"></a>    // handle space, add literal part (if exist), and add space part
<a name="line168"></a>    if (ch == &#39; &#39;) {
<a name="line169"></a>      if (buf.length &gt; 0) {
<a name="line170"></a>        this.patternParts_.push({text: buf, count: 0, abutStart: false});
<a name="line171"></a>        buf = &#39;&#39;;
<a name="line172"></a>      }
<a name="line173"></a>      this.patternParts_.push({text: &#39; &#39;, count: 0, abutStart: false});
<a name="line174"></a>      while (i &lt; pattern.length - 1 &amp;&amp; pattern.charAt(i + 1) == &#39; &#39;) {
<a name="line175"></a>        i++;
<a name="line176"></a>      }
<a name="line177"></a>    } else if (inQuote) {
<a name="line178"></a>      // inside quote, except &#39;&#39;, just copy or exit
<a name="line179"></a>      if (ch == &#39;\&#39;&#39;) {
<a name="line180"></a>        if (i + 1 &lt; pattern.length &amp;&amp; pattern.charAt(i + 1) == &#39;\&#39;&#39;) {
<a name="line181"></a>          // quote appeared twice continuously, interpret as one quote.
<a name="line182"></a>          buf += &#39;\&#39;&#39;;
<a name="line183"></a>          i++;
<a name="line184"></a>        } else {
<a name="line185"></a>          // exit quote
<a name="line186"></a>          inQuote = false;
<a name="line187"></a>        }
<a name="line188"></a>      } else {
<a name="line189"></a>        // literal
<a name="line190"></a>        buf += ch;
<a name="line191"></a>      }
<a name="line192"></a>    } else if (goog.i18n.DateTimeParse.PATTERN_CHARS_.indexOf(ch) &gt;= 0) {
<a name="line193"></a>      // outside quote, it is a pattern char
<a name="line194"></a>      if (buf.length &gt; 0) {
<a name="line195"></a>        this.patternParts_.push({text: buf, count: 0, abutStart: false});
<a name="line196"></a>        buf = &#39;&#39;;
<a name="line197"></a>      }
<a name="line198"></a>      var count = this.getNextCharCount_(pattern, i);
<a name="line199"></a>      this.patternParts_.push({text: ch, count: count, abutStart: false});
<a name="line200"></a>      i += count - 1;
<a name="line201"></a>    } else if (ch == &#39;\&#39;&#39;) {
<a name="line202"></a>      // Two consecutive quotes is a quote literal, inside or outside of quotes.
<a name="line203"></a>      if (i + 1 &lt; pattern.length &amp;&amp; pattern.charAt(i + 1) == &#39;\&#39;&#39;) {
<a name="line204"></a>        buf += &#39;\&#39;&#39;;
<a name="line205"></a>        i++;
<a name="line206"></a>      } else {
<a name="line207"></a>        inQuote = true;
<a name="line208"></a>      }
<a name="line209"></a>    } else {
<a name="line210"></a>      buf += ch;
<a name="line211"></a>    }
<a name="line212"></a>  }
<a name="line213"></a>
<a name="line214"></a>  if (buf.length &gt; 0) {
<a name="line215"></a>    this.patternParts_.push({text: buf, count: 0, abutStart: false});
<a name="line216"></a>  }
<a name="line217"></a>
<a name="line218"></a>  this.markAbutStart_();
<a name="line219"></a>};
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * Apply a predefined pattern to this Parser.
<a name="line224"></a> * @param {number} formatType A constant used to identified the predefined
<a name="line225"></a> *     pattern string stored in locale repository.
<a name="line226"></a> * @private
<a name="line227"></a> */
<a name="line228"></a>goog.i18n.DateTimeParse.prototype.applyStandardPattern_ = function(formatType) {
<a name="line229"></a>  var pattern;
<a name="line230"></a>  // formatType constants are in consecutive numbers. So it can be used to
<a name="line231"></a>  // index array in following way.
<a name="line232"></a>
<a name="line233"></a>  // if type is out of range, default to medium date/time format.
<a name="line234"></a>  if (formatType &gt; goog.i18n.DateTimeFormat.Format.SHORT_DATETIME) {
<a name="line235"></a>    formatType = goog.i18n.DateTimeFormat.Format.MEDIUM_DATETIME;
<a name="line236"></a>  }
<a name="line237"></a>
<a name="line238"></a>  if (formatType &lt; 4) {
<a name="line239"></a>    pattern = goog.i18n.DateTimeSymbols.DATEFORMATS[formatType];
<a name="line240"></a>  } else if (formatType &lt; 8) {
<a name="line241"></a>    pattern = goog.i18n.DateTimeSymbols.TIMEFORMATS[formatType - 4];
<a name="line242"></a>  } else {
<a name="line243"></a>    pattern = goog.i18n.DateTimeSymbols.DATETIMEFORMATS[formatType - 8];
<a name="line244"></a>    pattern = pattern.replace(&#39;{1}&#39;,
<a name="line245"></a>        goog.i18n.DateTimeSymbols.DATEFORMATS[formatType - 8]);
<a name="line246"></a>    pattern = pattern.replace(&#39;{0}&#39;,
<a name="line247"></a>        goog.i18n.DateTimeSymbols.TIMEFORMATS[formatType - 8]);
<a name="line248"></a>  }
<a name="line249"></a>  this.applyPattern_(pattern);
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/**
<a name="line254"></a> * Parse the given string and fill info into date object. This version does
<a name="line255"></a> * not validate the input.
<a name="line256"></a> * @param {string} text The string being parsed.
<a name="line257"></a> * @param {goog.date.DateLike} date The Date object to hold the parsed date.
<a name="line258"></a> * @param {number=} opt_start The position from where parse should begin.
<a name="line259"></a> * @return {number} How many characters parser advanced.
<a name="line260"></a> */
<a name="line261"></a>goog.i18n.DateTimeParse.prototype.parse = function(text, date, opt_start) {
<a name="line262"></a>  var start = opt_start || 0;
<a name="line263"></a>  return this.internalParse_(text, date, start, false/*validation*/);
<a name="line264"></a>};
<a name="line265"></a>
<a name="line266"></a>
<a name="line267"></a>/**
<a name="line268"></a> * Parse the given string and fill info into date object. This version will
<a name="line269"></a> * validate the input and make sure it is a validate date/time.
<a name="line270"></a> * @param {string} text The string being parsed.
<a name="line271"></a> * @param {goog.date.DateLike} date The Date object to hold the parsed date.
<a name="line272"></a> * @param {number=} opt_start The position from where parse should begin.
<a name="line273"></a> * @return {number} How many characters parser advanced.
<a name="line274"></a> */
<a name="line275"></a>goog.i18n.DateTimeParse.prototype.strictParse =
<a name="line276"></a>    function(text, date, opt_start) {
<a name="line277"></a>  var start = opt_start || 0;
<a name="line278"></a>  return this.internalParse_(text, date, start, true/*validation*/);
<a name="line279"></a>};
<a name="line280"></a>
<a name="line281"></a>
<a name="line282"></a>/**
<a name="line283"></a> * Parse the given string and fill info into date object.
<a name="line284"></a> * @param {string} text The string being parsed.
<a name="line285"></a> * @param {goog.date.DateLike} date The Date object to hold the parsed date.
<a name="line286"></a> * @param {number} start The position from where parse should begin.
<a name="line287"></a> * @param {boolean} validation If true, input string need to be a valid
<a name="line288"></a> *     date/time string.
<a name="line289"></a> * @return {number} How many characters parser advanced.
<a name="line290"></a> * @private
<a name="line291"></a> */
<a name="line292"></a>goog.i18n.DateTimeParse.prototype.internalParse_ =
<a name="line293"></a>    function(text, date, start, validation) {
<a name="line294"></a>  var cal = new goog.i18n.DateTimeParse.MyDate_();
<a name="line295"></a>  var parsePos = [start];
<a name="line296"></a>
<a name="line297"></a>  // For parsing abutting numeric fields. &#39;abutPat&#39; is the
<a name="line298"></a>  // offset into &#39;pattern&#39; of the first of 2 or more abutting
<a name="line299"></a>  // numeric fields. &#39;abutStart&#39; is the offset into &#39;text&#39;
<a name="line300"></a>  // where parsing the fields begins. &#39;abutPass&#39; starts off as 0
<a name="line301"></a>  // and increments each time we try to parse the fields.
<a name="line302"></a>  var abutPat = -1; // If &gt;=0, we are in a run of abutting numeric fields
<a name="line303"></a>  var abutStart = 0;
<a name="line304"></a>  var abutPass = 0;
<a name="line305"></a>
<a name="line306"></a>  for (var i = 0; i &lt; this.patternParts_.length; i++) {
<a name="line307"></a>    if (this.patternParts_[i].count &gt; 0) {
<a name="line308"></a>      if (abutPat &lt; 0 &amp;&amp; this.patternParts_[i].abutStart) {
<a name="line309"></a>        abutPat = i;
<a name="line310"></a>        abutStart = start;
<a name="line311"></a>        abutPass = 0;
<a name="line312"></a>      }
<a name="line313"></a>
<a name="line314"></a>      // Handle fields within a run of abutting numeric fields. Take
<a name="line315"></a>      // the pattern &quot;HHmmss&quot; as an example. We will try to parse
<a name="line316"></a>      // 2/2/2 characters of the input text, then if that fails,
<a name="line317"></a>      // 1/2/2. We only adjust the width of the leftmost field; the
<a name="line318"></a>      // others remain fixed. This allows &quot;123456&quot; =&gt; 12:34:56, but
<a name="line319"></a>      // &quot;12345&quot; =&gt; 1:23:45. Likewise, for the pattern &quot;yyyyMMdd&quot; we
<a name="line320"></a>      // try 4/2/2, 3/2/2, 2/2/2, and finally 1/2/2.
<a name="line321"></a>      if (abutPat &gt;= 0) {
<a name="line322"></a>        // If we are at the start of a run of abutting fields, then
<a name="line323"></a>        // shorten this field in each pass. If we can&#39;t shorten
<a name="line324"></a>        // this field any more, then the parse of this set of
<a name="line325"></a>        // abutting numeric fields has failed.
<a name="line326"></a>        var count = this.patternParts_[i].count;
<a name="line327"></a>        if (i == abutPat) {
<a name="line328"></a>          count -= abutPass;
<a name="line329"></a>          abutPass++;
<a name="line330"></a>          if (count == 0) {
<a name="line331"></a>            // tried all possible width, fail now
<a name="line332"></a>            return 0;
<a name="line333"></a>          }
<a name="line334"></a>        }
<a name="line335"></a>
<a name="line336"></a>        if (!this.subParse_(text, parsePos, this.patternParts_[i], count,
<a name="line337"></a>                            cal)) {
<a name="line338"></a>          // If the parse fails anywhere in the run, back up to the
<a name="line339"></a>          // start of the run and retry.
<a name="line340"></a>          i = abutPat - 1;
<a name="line341"></a>          parsePos[0] = abutStart;
<a name="line342"></a>          continue;
<a name="line343"></a>        }
<a name="line344"></a>      }
<a name="line345"></a>
<a name="line346"></a>      // Handle non-numeric fields and non-abutting numeric fields.
<a name="line347"></a>      else {
<a name="line348"></a>        abutPat = -1;
<a name="line349"></a>        if (!this.subParse_(text, parsePos, this.patternParts_[i], 0, cal)) {
<a name="line350"></a>          return 0;
<a name="line351"></a>        }
<a name="line352"></a>      }
<a name="line353"></a>    } else {
<a name="line354"></a>      // Handle literal pattern characters. These are any
<a name="line355"></a>      // quoted characters and non-alphabetic unquoted
<a name="line356"></a>      // characters.
<a name="line357"></a>      abutPat = -1;
<a name="line358"></a>      // A run of white space in the pattern matches a run
<a name="line359"></a>      // of white space in the input text.
<a name="line360"></a>      if (this.patternParts_[i].text.charAt(0) == &#39; &#39;) {
<a name="line361"></a>        // Advance over run in input text
<a name="line362"></a>        var s = parsePos[0];
<a name="line363"></a>        this.skipSpace_(text, parsePos);
<a name="line364"></a>
<a name="line365"></a>        // Must see at least one white space char in input
<a name="line366"></a>        if (parsePos[0] &gt; s) {
<a name="line367"></a>          continue;
<a name="line368"></a>        }
<a name="line369"></a>      } else if (text.indexOf(this.patternParts_[i].text, parsePos[0]) ==
<a name="line370"></a>                 parsePos[0]) {
<a name="line371"></a>        parsePos[0] += this.patternParts_[i].text.length;
<a name="line372"></a>        continue;
<a name="line373"></a>      }
<a name="line374"></a>      // We fall through to this point if the match fails
<a name="line375"></a>      return 0;
<a name="line376"></a>    }
<a name="line377"></a>  }
<a name="line378"></a>
<a name="line379"></a>  // return progress
<a name="line380"></a>  return cal.calcDate_(date, validation) ? parsePos[0] - start : 0;
<a name="line381"></a>};
<a name="line382"></a>
<a name="line383"></a>
<a name="line384"></a>/**
<a name="line385"></a> * Calculate character repeat count in pattern.
<a name="line386"></a> *
<a name="line387"></a> * @param {string} pattern It describes the format of date string that need to
<a name="line388"></a> *     be parsed.
<a name="line389"></a> * @param {number} start The position of pattern character.
<a name="line390"></a> *
<a name="line391"></a> * @return {number} Repeat count.
<a name="line392"></a> * @private
<a name="line393"></a> */
<a name="line394"></a>goog.i18n.DateTimeParse.prototype.getNextCharCount_ =
<a name="line395"></a>    function(pattern, start) {
<a name="line396"></a>  var ch = pattern.charAt(start);
<a name="line397"></a>  var next = start + 1;
<a name="line398"></a>  while (next &lt; pattern.length &amp;&amp; pattern.charAt(next) == ch) {
<a name="line399"></a>    next++;
<a name="line400"></a>  }
<a name="line401"></a>  return next - start;
<a name="line402"></a>};
<a name="line403"></a>
<a name="line404"></a>
<a name="line405"></a>/**
<a name="line406"></a> * All acceptable pattern characters.
<a name="line407"></a> * @private
<a name="line408"></a> */
<a name="line409"></a>goog.i18n.DateTimeParse.PATTERN_CHARS_ = &#39;GyMdkHmsSEDahKzZvQL&#39;;
<a name="line410"></a>
<a name="line411"></a>
<a name="line412"></a>/**
<a name="line413"></a> * Pattern characters that specify numerical field.
<a name="line414"></a> * @private
<a name="line415"></a> */
<a name="line416"></a>goog.i18n.DateTimeParse.NUMERIC_FORMAT_CHARS_ = &#39;MydhHmsSDkK&#39;;
<a name="line417"></a>
<a name="line418"></a>
<a name="line419"></a>/**
<a name="line420"></a> * Check if the pattern part is a numeric field.
<a name="line421"></a> *
<a name="line422"></a> * @param {Object} part pattern part to be examined.
<a name="line423"></a> *
<a name="line424"></a> * @return {boolean} true if the pattern part is numeric field.
<a name="line425"></a> * @private
<a name="line426"></a> */
<a name="line427"></a>goog.i18n.DateTimeParse.prototype.isNumericField_ = function(part) {
<a name="line428"></a>  if (part.count &lt;= 0) {
<a name="line429"></a>    return false;
<a name="line430"></a>  }
<a name="line431"></a>  var i = goog.i18n.DateTimeParse.NUMERIC_FORMAT_CHARS_.indexOf(
<a name="line432"></a>      part.text.charAt(0));
<a name="line433"></a>  return i &gt; 0 || i == 0 &amp;&amp; part.count &lt; 3;
<a name="line434"></a>};
<a name="line435"></a>
<a name="line436"></a>
<a name="line437"></a>/**
<a name="line438"></a> * Identify the start of an abutting numeric fields&#39; run. Taking pattern
<a name="line439"></a> * &quot;HHmmss&quot; as an example. It will try to parse 2/2/2 characters of the input
<a name="line440"></a> * text, then if that fails, 1/2/2. We only adjust the width of the leftmost
<a name="line441"></a> * field; the others remain fixed. This allows &quot;123456&quot; =&gt; 12:34:56, but
<a name="line442"></a> * &quot;12345&quot; =&gt; 1:23:45. Likewise, for the pattern &quot;yyyyMMdd&quot; we try 4/2/2,
<a name="line443"></a> * 3/2/2, 2/2/2, and finally 1/2/2. The first field of connected numeric
<a name="line444"></a> * fields will be marked as abutStart, its width can be reduced to accommodate
<a name="line445"></a> * others.
<a name="line446"></a> *
<a name="line447"></a> * @private
<a name="line448"></a> */
<a name="line449"></a>goog.i18n.DateTimeParse.prototype.markAbutStart_ = function() {
<a name="line450"></a>  // abut parts are continuous numeric parts. abutStart is the switch
<a name="line451"></a>  // point from non-abut to abut
<a name="line452"></a>  var abut = false;
<a name="line453"></a>
<a name="line454"></a>  for (var i = 0; i &lt; this.patternParts_.length; i++) {
<a name="line455"></a>    if (this.isNumericField_(this.patternParts_[i])) {
<a name="line456"></a>      // if next part is not following abut sequence, and isNumericField_
<a name="line457"></a>      if (!abut &amp;&amp; i + 1 &lt; this.patternParts_.length &amp;&amp;
<a name="line458"></a>          this.isNumericField_(this.patternParts_[i + 1])) {
<a name="line459"></a>        abut = true;
<a name="line460"></a>        this.patternParts_[i].abutStart = true;
<a name="line461"></a>      }
<a name="line462"></a>    } else {
<a name="line463"></a>      abut = false;
<a name="line464"></a>    }
<a name="line465"></a>  }
<a name="line466"></a>};
<a name="line467"></a>
<a name="line468"></a>
<a name="line469"></a>/**
<a name="line470"></a> * Skip space in the string.
<a name="line471"></a> *
<a name="line472"></a> * @param {string} text input string.
<a name="line473"></a> * @param {Array.&lt;number&gt;} pos where skip start, and return back where the skip
<a name="line474"></a> *     stops.
<a name="line475"></a> * @private
<a name="line476"></a> */
<a name="line477"></a>goog.i18n.DateTimeParse.prototype.skipSpace_ = function(text, pos) {
<a name="line478"></a>  var m = text.substring(pos[0]).match(/^\s+/);
<a name="line479"></a>  if (m) {
<a name="line480"></a>    pos[0] += m[0].length;
<a name="line481"></a>  }
<a name="line482"></a>};
<a name="line483"></a>
<a name="line484"></a>
<a name="line485"></a>/**
<a name="line486"></a> * Protected method that converts one field of the input string into a
<a name="line487"></a> * numeric field value.
<a name="line488"></a> *
<a name="line489"></a> * @param {string} text the time text to be parsed.
<a name="line490"></a> * @param {Array.&lt;number&gt;} pos Parse position.
<a name="line491"></a> * @param {Object} part the pattern part for this field.
<a name="line492"></a> * @param {number} digitCount when &gt; 0, numeric parsing must obey the count.
<a name="line493"></a> * @param {goog.i18n.DateTimeParse.MyDate_} cal object that holds parsed value.
<a name="line494"></a> *
<a name="line495"></a> * @return {boolean} True if it parses successfully.
<a name="line496"></a> * @private
<a name="line497"></a> */
<a name="line498"></a>goog.i18n.DateTimeParse.prototype.subParse_ =
<a name="line499"></a>    function(text, pos, part, digitCount, cal) {
<a name="line500"></a>  this.skipSpace_(text, pos);
<a name="line501"></a>
<a name="line502"></a>  var start = pos[0];
<a name="line503"></a>  var ch = part.text.charAt(0);
<a name="line504"></a>
<a name="line505"></a>  // parse integer value if it is a numeric field
<a name="line506"></a>  var value = -1;
<a name="line507"></a>  if (this.isNumericField_(part)) {
<a name="line508"></a>    if (digitCount &gt; 0) {
<a name="line509"></a>      if ((start + digitCount) &gt; text.length) {
<a name="line510"></a>        return false;
<a name="line511"></a>      }
<a name="line512"></a>      value = this.parseInt_(
<a name="line513"></a>          text.substring(0, start + digitCount), pos);
<a name="line514"></a>    } else {
<a name="line515"></a>      value = this.parseInt_(text, pos);
<a name="line516"></a>    }
<a name="line517"></a>  }
<a name="line518"></a>
<a name="line519"></a>  switch (ch) {
<a name="line520"></a>    case &#39;G&#39;: // ERA
<a name="line521"></a>      cal.era = this.matchString_(text, pos, goog.i18n.DateTimeSymbols.ERAS);
<a name="line522"></a>      return true;
<a name="line523"></a>    case &#39;M&#39;: // MONTH
<a name="line524"></a>    case &#39;L&#39;: // STANDALONEMONTH
<a name="line525"></a>      return this.subParseMonth_(text, pos, cal, value);
<a name="line526"></a>    case &#39;E&#39;:
<a name="line527"></a>      return this.subParseDayOfWeek_(text, pos, cal);
<a name="line528"></a>    case &#39;a&#39;: // AM_PM
<a name="line529"></a>      cal.ampm = this.matchString_(text, pos, goog.i18n.DateTimeSymbols.AMPMS);
<a name="line530"></a>      return true;
<a name="line531"></a>    case &#39;y&#39;: // YEAR
<a name="line532"></a>      return this.subParseYear_(text, pos, start, value, part, cal);
<a name="line533"></a>    case &#39;Q&#39;: // QUARTER
<a name="line534"></a>      return this.subParseQuarter_(text, pos, cal, value);
<a name="line535"></a>    case &#39;d&#39;: // DATE
<a name="line536"></a>      cal.day = value;
<a name="line537"></a>      return true;
<a name="line538"></a>    case &#39;S&#39;: // FRACTIONAL_SECOND
<a name="line539"></a>      return this.subParseFractionalSeconds_(value, pos, start, cal);
<a name="line540"></a>    case &#39;h&#39;: // HOUR (1..12)
<a name="line541"></a>      if (value == 12) {
<a name="line542"></a>        value = 0;
<a name="line543"></a>      }
<a name="line544"></a>    case &#39;K&#39;: // HOUR (0..11)
<a name="line545"></a>    case &#39;H&#39;: // HOUR_OF_DAY (0..23)
<a name="line546"></a>    case &#39;k&#39;: // HOUR_OF_DAY (1..24)
<a name="line547"></a>      cal.hours = value;
<a name="line548"></a>      return true;
<a name="line549"></a>    case &#39;m&#39;: // MINUTE
<a name="line550"></a>      cal.minutes = value;
<a name="line551"></a>      return true;
<a name="line552"></a>    case &#39;s&#39;: // SECOND
<a name="line553"></a>      cal.seconds = value;
<a name="line554"></a>      return true;
<a name="line555"></a>
<a name="line556"></a>    case &#39;z&#39;: // ZONE_OFFSET
<a name="line557"></a>    case &#39;Z&#39;: // TIMEZONE_RFC
<a name="line558"></a>    case &#39;v&#39;: // TIMEZONE_GENERIC
<a name="line559"></a>      return this.subparseTimeZoneInGMT_(text, pos, cal);
<a name="line560"></a>    default:
<a name="line561"></a>      return false;
<a name="line562"></a>  }
<a name="line563"></a>};
<a name="line564"></a>
<a name="line565"></a>
<a name="line566"></a>/**
<a name="line567"></a> * Parse year field. Year field is special because
<a name="line568"></a> * 1) two digit year need to be resolved.
<a name="line569"></a> * 2) we allow year to take a sign.
<a name="line570"></a> * 3) year field participate in abut processing.
<a name="line571"></a> *
<a name="line572"></a> * @param {string} text the time text to be parsed.
<a name="line573"></a> * @param {Array.&lt;number&gt;} pos Parse position.
<a name="line574"></a> * @param {number} start where this field start.
<a name="line575"></a> * @param {number} value integer value of year.
<a name="line576"></a> * @param {Object} part the pattern part for this field.
<a name="line577"></a> * @param {goog.i18n.DateTimeParse.MyDate_} cal object to hold parsed value.
<a name="line578"></a> *
<a name="line579"></a> * @return {boolean} True if successful.
<a name="line580"></a> * @private
<a name="line581"></a> */
<a name="line582"></a>goog.i18n.DateTimeParse.prototype.subParseYear_ =
<a name="line583"></a>    function(text, pos, start, value, part, cal) {
<a name="line584"></a>  var ch;
<a name="line585"></a>  if (value &lt; 0) {
<a name="line586"></a>    //possible sign
<a name="line587"></a>    ch = text.charAt(pos[0]);
<a name="line588"></a>    if (ch != &#39;+&#39; &amp;&amp; ch != &#39;-&#39;) {
<a name="line589"></a>      return false;
<a name="line590"></a>    }
<a name="line591"></a>    pos[0]++;
<a name="line592"></a>    value = this.parseInt_(text, pos);
<a name="line593"></a>    if (value &lt; 0) {
<a name="line594"></a>      return false;
<a name="line595"></a>    }
<a name="line596"></a>    if (ch == &#39;-&#39;) {
<a name="line597"></a>      value = -value;
<a name="line598"></a>    }
<a name="line599"></a>  }
<a name="line600"></a>
<a name="line601"></a>  // only if 2 digit was actually parsed, and pattern say it has 2 digit.
<a name="line602"></a>  if (!ch &amp;&amp; pos[0] - start == 2 &amp;&amp; part.count == 2) {
<a name="line603"></a>    cal.setTwoDigitYear_(value);
<a name="line604"></a>  } else {
<a name="line605"></a>    cal.year = value;
<a name="line606"></a>  }
<a name="line607"></a>  return true;
<a name="line608"></a>};
<a name="line609"></a>
<a name="line610"></a>
<a name="line611"></a>/**
<a name="line612"></a> * Parse Month field.
<a name="line613"></a> *
<a name="line614"></a> * @param {string} text the time text to be parsed.
<a name="line615"></a> * @param {Array.&lt;number&gt;} pos Parse position.
<a name="line616"></a> * @param {goog.i18n.DateTimeParse.MyDate_} cal object to hold parsed value.
<a name="line617"></a> * @param {number} value numeric value if this field is expressed using
<a name="line618"></a> *      numeric pattern, or -1 if not.
<a name="line619"></a> *
<a name="line620"></a> * @return {boolean} True if parsing successful.
<a name="line621"></a> * @private
<a name="line622"></a> */
<a name="line623"></a>goog.i18n.DateTimeParse.prototype.subParseMonth_ =
<a name="line624"></a>    function(text, pos, cal, value) {
<a name="line625"></a>  // when month is symbols, i.e., MMM, MMMM, LLL or LLLL, value will be -1
<a name="line626"></a>  if (value &lt; 0) {
<a name="line627"></a>    // Want to be able to parse both short and long forms.
<a name="line628"></a>    // Try count == 4 first
<a name="line629"></a>    var months = goog.i18n.DateTimeSymbols.MONTHS.concat(
<a name="line630"></a>        goog.i18n.DateTimeSymbols.STANDALONEMONTHS).concat(
<a name="line631"></a>        goog.i18n.DateTimeSymbols.SHORTMONTHS).concat(
<a name="line632"></a>        goog.i18n.DateTimeSymbols.STANDALONESHORTMONTHS);
<a name="line633"></a>    value = this.matchString_(text, pos, months);
<a name="line634"></a>    if (value &lt; 0) {
<a name="line635"></a>      return false;
<a name="line636"></a>    }
<a name="line637"></a>    // The months variable is multiple of 12, so we have to get the actual
<a name="line638"></a>    // month index by modulo 12.
<a name="line639"></a>    cal.month = (value % 12);
<a name="line640"></a>    return true;
<a name="line641"></a>  } else {
<a name="line642"></a>    cal.month = value - 1;
<a name="line643"></a>    return true;
<a name="line644"></a>  }
<a name="line645"></a>};
<a name="line646"></a>
<a name="line647"></a>
<a name="line648"></a>/**
<a name="line649"></a> * Parse Quarter field.
<a name="line650"></a> *
<a name="line651"></a> * @param {string} text the time text to be parsed.
<a name="line652"></a> * @param {Array.&lt;number&gt;} pos Parse position.
<a name="line653"></a> * @param {goog.i18n.DateTimeParse.MyDate_} cal object to hold parsed value.
<a name="line654"></a> * @param {number} value numeric value if this field is expressed using
<a name="line655"></a> *      numeric pattern, or -1 if not.
<a name="line656"></a> *
<a name="line657"></a> * @return {boolean} True if parsing successful.
<a name="line658"></a> * @private
<a name="line659"></a> */
<a name="line660"></a>goog.i18n.DateTimeParse.prototype.subParseQuarter_ =
<a name="line661"></a>    function(text, pos, cal, value) {
<a name="line662"></a>  // value should be -1, since this is a non-numeric field.
<a name="line663"></a>  if (value &lt; 0) {
<a name="line664"></a>    // Want to be able to parse both short and long forms.
<a name="line665"></a>    // Try count == 4 first:
<a name="line666"></a>    value = this.matchString_(text, pos, goog.i18n.DateTimeSymbols.QUARTERS);
<a name="line667"></a>    if (value &lt; 0) { // count == 4 failed, now try count == 3
<a name="line668"></a>      value = this.matchString_(text, pos,
<a name="line669"></a>                                goog.i18n.DateTimeSymbols.SHORTQUARTERS);
<a name="line670"></a>    }
<a name="line671"></a>    if (value &lt; 0) {
<a name="line672"></a>      return false;
<a name="line673"></a>    }
<a name="line674"></a>    cal.month = value * 3;  // First month of quarter.
<a name="line675"></a>    cal.day = 1;
<a name="line676"></a>    return true;
<a name="line677"></a>  }
<a name="line678"></a>  return false;
<a name="line679"></a>};
<a name="line680"></a>
<a name="line681"></a>
<a name="line682"></a>/**
<a name="line683"></a> * Parse Day of week field.
<a name="line684"></a> * @param {string} text the time text to be parsed.
<a name="line685"></a> * @param {Array.&lt;number&gt;} pos Parse position.
<a name="line686"></a> * @param {goog.i18n.DateTimeParse.MyDate_} cal object to hold parsed value.
<a name="line687"></a> *
<a name="line688"></a> * @return {boolean} True if successful.
<a name="line689"></a> * @private
<a name="line690"></a> */
<a name="line691"></a>goog.i18n.DateTimeParse.prototype.subParseDayOfWeek_ =
<a name="line692"></a>    function(text, pos, cal) {
<a name="line693"></a>  // Handle both short and long forms.
<a name="line694"></a>  // Try count == 4 (DDDD) first:
<a name="line695"></a>  var value = this.matchString_(text, pos, goog.i18n.DateTimeSymbols.WEEKDAYS);
<a name="line696"></a>  if (value &lt; 0) {
<a name="line697"></a>    value = this.matchString_(text, pos,
<a name="line698"></a>                              goog.i18n.DateTimeSymbols.SHORTWEEKDAYS);
<a name="line699"></a>  }
<a name="line700"></a>  if (value &lt; 0) {
<a name="line701"></a>    return false;
<a name="line702"></a>  }
<a name="line703"></a>  cal.dayOfWeek = value;
<a name="line704"></a>  return true;
<a name="line705"></a>};
<a name="line706"></a>
<a name="line707"></a>
<a name="line708"></a>/**
<a name="line709"></a> * Parse fractional seconds field.
<a name="line710"></a> *
<a name="line711"></a> * @param {number} value parsed numeric value.
<a name="line712"></a> * @param {Array.&lt;number&gt;} pos current parse position.
<a name="line713"></a> * @param {number} start where this field start.
<a name="line714"></a> * @param {goog.i18n.DateTimeParse.MyDate_} cal object to hold parsed value.
<a name="line715"></a> *
<a name="line716"></a> * @return {boolean} True if successful.
<a name="line717"></a> * @private
<a name="line718"></a> */
<a name="line719"></a>goog.i18n.DateTimeParse.prototype.subParseFractionalSeconds_ =
<a name="line720"></a>    function(value, pos, start, cal) {
<a name="line721"></a>  // Fractional seconds left-justify
<a name="line722"></a>  var len = pos[0] - start;
<a name="line723"></a>  cal.milliseconds = len &lt; 3 ? value * Math.pow(10, 3 - len) :
<a name="line724"></a>                               Math.round(value / Math.pow(10, len - 3));
<a name="line725"></a>  return true;
<a name="line726"></a>};
<a name="line727"></a>
<a name="line728"></a>
<a name="line729"></a>/**
<a name="line730"></a> * Parse GMT type timezone.
<a name="line731"></a> *
<a name="line732"></a> * @param {string} text the time text to be parsed.
<a name="line733"></a> * @param {Array.&lt;number&gt;} pos Parse position.
<a name="line734"></a> * @param {goog.i18n.DateTimeParse.MyDate_} cal object to hold parsed value.
<a name="line735"></a> *
<a name="line736"></a> * @return {boolean} True if successful.
<a name="line737"></a> * @private
<a name="line738"></a> */
<a name="line739"></a>goog.i18n.DateTimeParse.prototype.subparseTimeZoneInGMT_ =
<a name="line740"></a>    function(text, pos, cal) {
<a name="line741"></a>  // First try to parse generic forms such as GMT-07:00. Do this first
<a name="line742"></a>  // in case localized DateFormatZoneData contains the string &quot;GMT&quot;
<a name="line743"></a>  // for a zone; in that case, we don&#39;t want to match the first three
<a name="line744"></a>  // characters of GMT+/-HH:MM etc.
<a name="line745"></a>
<a name="line746"></a>  // For time zones that have no known names, look for strings
<a name="line747"></a>  // of the form:
<a name="line748"></a>  //    GMT[+-]hours:minutes or
<a name="line749"></a>  //    GMT[+-]hhmm or
<a name="line750"></a>  //    GMT.
<a name="line751"></a>  if (text.indexOf(&#39;GMT&#39;, pos[0]) == pos[0]) {
<a name="line752"></a>    pos[0] += 3;  // 3 is the length of GMT
<a name="line753"></a>    return this.parseTimeZoneOffset_(text, pos, cal);
<a name="line754"></a>  }
<a name="line755"></a>
<a name="line756"></a>  // TODO(user): check for named time zones by looking through the locale
<a name="line757"></a>  // data from the DateFormatZoneData strings. Should parse both short and long
<a name="line758"></a>  // forms.
<a name="line759"></a>  // subParseZoneString(text, start, cal);
<a name="line760"></a>
<a name="line761"></a>  // As a last resort, look for numeric timezones of the form
<a name="line762"></a>  // [+-]hhmm as specified by RFC 822.  This code is actually
<a name="line763"></a>  // a little more permissive than RFC 822.  It will try to do
<a name="line764"></a>  // its best with numbers that aren&#39;t strictly 4 digits long.
<a name="line765"></a>  return this.parseTimeZoneOffset_(text, pos, cal);
<a name="line766"></a>};
<a name="line767"></a>
<a name="line768"></a>
<a name="line769"></a>/**
<a name="line770"></a> * Parse time zone offset.
<a name="line771"></a> *
<a name="line772"></a> * @param {string} text the time text to be parsed.
<a name="line773"></a> * @param {Array.&lt;number&gt;} pos Parse position.
<a name="line774"></a> * @param {goog.i18n.DateTimeParse.MyDate_} cal object to hold parsed value.
<a name="line775"></a> *
<a name="line776"></a> * @return {boolean} True if successful.
<a name="line777"></a> * @private
<a name="line778"></a> */
<a name="line779"></a>goog.i18n.DateTimeParse.prototype.parseTimeZoneOffset_ =
<a name="line780"></a>    function(text, pos, cal) {
<a name="line781"></a>  if (pos[0] &gt;= text.length) {
<a name="line782"></a>    cal.tzOffset = 0;
<a name="line783"></a>    return true;
<a name="line784"></a>  }
<a name="line785"></a>
<a name="line786"></a>  var sign = 1;
<a name="line787"></a>  switch (text.charAt(pos[0])) {
<a name="line788"></a>    case &#39;-&#39;: sign = -1;  // fall through
<a name="line789"></a>    case &#39;+&#39;: pos[0]++;
<a name="line790"></a>  }
<a name="line791"></a>
<a name="line792"></a>  // Look for hours:minutes or hhmm.
<a name="line793"></a>  var st = pos[0];
<a name="line794"></a>  var value = this.parseInt_(text, pos);
<a name="line795"></a>  if (value == 0 &amp;&amp; pos[0] == st) {
<a name="line796"></a>    return false;
<a name="line797"></a>  }
<a name="line798"></a>
<a name="line799"></a>  var offset;
<a name="line800"></a>  if (pos[0] &lt; text.length &amp;&amp; text.charAt(pos[0]) == &#39;:&#39;) {
<a name="line801"></a>    // This is the hours:minutes case
<a name="line802"></a>    offset = value * 60;
<a name="line803"></a>    pos[0]++;
<a name="line804"></a>    st = pos[0];
<a name="line805"></a>    value = this.parseInt_(text, pos);
<a name="line806"></a>    if (value == 0 &amp;&amp; pos[0] == st) {
<a name="line807"></a>      return false;
<a name="line808"></a>    }
<a name="line809"></a>    offset += value;
<a name="line810"></a>  } else {
<a name="line811"></a>    // This is the hhmm case.
<a name="line812"></a>    offset = value;
<a name="line813"></a>    // Assume &quot;-23&quot;..&quot;+23&quot; refers to hours.
<a name="line814"></a>    if (offset &lt; 24 &amp;&amp; (pos[0] - st) &lt;= 2) {
<a name="line815"></a>      offset *= 60;
<a name="line816"></a>    } else {
<a name="line817"></a>      // todo: this looks questionable, should have more error checking
<a name="line818"></a>      offset = offset % 100 + offset / 100 * 60;
<a name="line819"></a>    }
<a name="line820"></a>  }
<a name="line821"></a>
<a name="line822"></a>  offset *= sign;
<a name="line823"></a>  cal.tzOffset = -offset;
<a name="line824"></a>  return true;
<a name="line825"></a>};
<a name="line826"></a>
<a name="line827"></a>
<a name="line828"></a>/**
<a name="line829"></a> * Parse a integer string and return integer value.
<a name="line830"></a> *
<a name="line831"></a> * @param {string} text string being parsed.
<a name="line832"></a> * @param {Array.&lt;number&gt;} pos parse position.
<a name="line833"></a> *
<a name="line834"></a> * @return {number} Converted integer value.
<a name="line835"></a> * @private
<a name="line836"></a> */
<a name="line837"></a>goog.i18n.DateTimeParse.prototype.parseInt_ = function(text, pos) {
<a name="line838"></a>  // Delocalizes the string containing native digits specified by the locale,
<a name="line839"></a>  // replaces the native digits with ASCII digits. Leaves other characters.
<a name="line840"></a>  // This is the reverse operation of localizeNumbers_ in datetimeformat.js.
<a name="line841"></a>  if (goog.i18n.DateTimeSymbols.ZERODIGIT) {
<a name="line842"></a>    var parts = [];
<a name="line843"></a>    for (var i = pos[0]; i &lt; text.length; i++) {
<a name="line844"></a>      var c = text.charCodeAt(i) - goog.i18n.DateTimeSymbols.ZERODIGIT;
<a name="line845"></a>      parts.push((0 &lt;= c &amp;&amp; c &lt;= 9) ?
<a name="line846"></a>          String.fromCharCode(c + 0x30) :
<a name="line847"></a>          text.charAt(i));
<a name="line848"></a>    }
<a name="line849"></a>    text = parts.join(&#39;&#39;);
<a name="line850"></a>  } else {
<a name="line851"></a>    text = text.substring(pos[0]);
<a name="line852"></a>  }
<a name="line853"></a>
<a name="line854"></a>  var m = text.match(/^\d+/);
<a name="line855"></a>  if (!m) {
<a name="line856"></a>    return -1;
<a name="line857"></a>  }
<a name="line858"></a>  pos[0] += m[0].length;
<a name="line859"></a>  return parseInt(m[0], 10);
<a name="line860"></a>};
<a name="line861"></a>
<a name="line862"></a>
<a name="line863"></a>/**
<a name="line864"></a> * Attempt to match the text at a given position against an array of strings.
<a name="line865"></a> * Since multiple strings in the array may match (for example, if the array
<a name="line866"></a> * contains &quot;a&quot;, &quot;ab&quot;, and &quot;abc&quot;, all will match the input string &quot;abcd&quot;) the
<a name="line867"></a> * longest match is returned.
<a name="line868"></a> *
<a name="line869"></a> * @param {string} text The string to match to.
<a name="line870"></a> * @param {Array.&lt;number&gt;} pos parsing position.
<a name="line871"></a> * @param {Array.&lt;string&gt;} data The string array of matching patterns.
<a name="line872"></a> *
<a name="line873"></a> * @return {number} the new start position if matching succeeded; a negative
<a name="line874"></a> *     number indicating matching failure.
<a name="line875"></a> * @private
<a name="line876"></a> */
<a name="line877"></a>goog.i18n.DateTimeParse.prototype.matchString_ = function(text, pos, data) {
<a name="line878"></a>  // There may be multiple strings in the data[] array which begin with
<a name="line879"></a>  // the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
<a name="line880"></a>  // We keep track of the longest match, and return that. Note that this
<a name="line881"></a>  // unfortunately requires us to test all array elements.
<a name="line882"></a>  var bestMatchLength = 0;
<a name="line883"></a>  var bestMatch = -1;
<a name="line884"></a>  var lower_text = text.substring(pos[0]).toLowerCase();
<a name="line885"></a>  for (var i = 0; i &lt; data.length; i++) {
<a name="line886"></a>    var len = data[i].length;
<a name="line887"></a>    // Always compare if we have no match yet; otherwise only compare
<a name="line888"></a>    // against potentially better matches (longer strings).
<a name="line889"></a>    if (len &gt; bestMatchLength &amp;&amp;
<a name="line890"></a>        lower_text.indexOf(data[i].toLowerCase()) == 0) {
<a name="line891"></a>      bestMatch = i;
<a name="line892"></a>      bestMatchLength = len;
<a name="line893"></a>    }
<a name="line894"></a>  }
<a name="line895"></a>  if (bestMatch &gt;= 0) {
<a name="line896"></a>    pos[0] += bestMatchLength;
<a name="line897"></a>  }
<a name="line898"></a>  return bestMatch;
<a name="line899"></a>};
<a name="line900"></a>
<a name="line901"></a>
<a name="line902"></a>
<a name="line903"></a>/**
<a name="line904"></a> * This class hold the intermediate parsing result. After all fields are
<a name="line905"></a> * consumed, final result will be resolved from this class.
<a name="line906"></a> * @constructor
<a name="line907"></a> * @private
<a name="line908"></a> */
<a name="line909"></a>goog.i18n.DateTimeParse.MyDate_ = function() {};
<a name="line910"></a>
<a name="line911"></a>
<a name="line912"></a>/**
<a name="line913"></a> * The date&#39;s era.
<a name="line914"></a> * @type {?number}
<a name="line915"></a> */
<a name="line916"></a>goog.i18n.DateTimeParse.MyDate_.prototype.era;
<a name="line917"></a>
<a name="line918"></a>
<a name="line919"></a>/**
<a name="line920"></a> * The date&#39;s year.
<a name="line921"></a> * @type {?number}
<a name="line922"></a> */
<a name="line923"></a>goog.i18n.DateTimeParse.MyDate_.prototype.year;
<a name="line924"></a>
<a name="line925"></a>
<a name="line926"></a>/**
<a name="line927"></a> * The date&#39;s month.
<a name="line928"></a> * @type {?number}
<a name="line929"></a> */
<a name="line930"></a>goog.i18n.DateTimeParse.MyDate_.prototype.month;
<a name="line931"></a>
<a name="line932"></a>
<a name="line933"></a>/**
<a name="line934"></a> * The date&#39;s day of month.
<a name="line935"></a> * @type {?number}
<a name="line936"></a> */
<a name="line937"></a>goog.i18n.DateTimeParse.MyDate_.prototype.day;
<a name="line938"></a>
<a name="line939"></a>
<a name="line940"></a>/**
<a name="line941"></a> * The date&#39;s hour.
<a name="line942"></a> * @type {?number}
<a name="line943"></a> */
<a name="line944"></a>goog.i18n.DateTimeParse.MyDate_.prototype.hours;
<a name="line945"></a>
<a name="line946"></a>
<a name="line947"></a>/**
<a name="line948"></a> * The date&#39;s before/afternoon denominator.
<a name="line949"></a> * @type {?number}
<a name="line950"></a> */
<a name="line951"></a>goog.i18n.DateTimeParse.MyDate_.prototype.ampm;
<a name="line952"></a>
<a name="line953"></a>
<a name="line954"></a>/**
<a name="line955"></a> * The date&#39;s minutes.
<a name="line956"></a> * @type {?number}
<a name="line957"></a> */
<a name="line958"></a>goog.i18n.DateTimeParse.MyDate_.prototype.minutes;
<a name="line959"></a>
<a name="line960"></a>
<a name="line961"></a>/**
<a name="line962"></a> * The date&#39;s seconds.
<a name="line963"></a> * @type {?number}
<a name="line964"></a> */
<a name="line965"></a>goog.i18n.DateTimeParse.MyDate_.prototype.seconds;
<a name="line966"></a>
<a name="line967"></a>
<a name="line968"></a>/**
<a name="line969"></a> * The date&#39;s milliseconds.
<a name="line970"></a> * @type {?number}
<a name="line971"></a> */
<a name="line972"></a>goog.i18n.DateTimeParse.MyDate_.prototype.milliseconds;
<a name="line973"></a>
<a name="line974"></a>
<a name="line975"></a>/**
<a name="line976"></a> * The date&#39;s timezone offset.
<a name="line977"></a> * @type {?number}
<a name="line978"></a> */
<a name="line979"></a>goog.i18n.DateTimeParse.MyDate_.prototype.tzOffset;
<a name="line980"></a>
<a name="line981"></a>
<a name="line982"></a>/**
<a name="line983"></a> * The date&#39;s day of week. Sunday is 0, Saturday is 6.
<a name="line984"></a> * @type {?number}
<a name="line985"></a> */
<a name="line986"></a>goog.i18n.DateTimeParse.MyDate_.prototype.dayOfWeek;
<a name="line987"></a>
<a name="line988"></a>
<a name="line989"></a>/**
<a name="line990"></a> * 2 digit year special handling. Assuming for example that the
<a name="line991"></a> * defaultCenturyStart is 6/18/1903. This means that two-digit years will be
<a name="line992"></a> * forced into the range 6/18/1903 to 6/17/2003. As a result, years 00, 01, and
<a name="line993"></a> * 02 correspond to 2000, 2001, and 2002. Years 04, 05, etc. correspond
<a name="line994"></a> * to 1904, 1905, etc. If the year is 03, then it is 2003 if the
<a name="line995"></a> * other fields specify a date before 6/18, or 1903 if they specify a
<a name="line996"></a> * date afterwards. As a result, 03 is an ambiguous year. All other
<a name="line997"></a> * two-digit years are unambiguous.
<a name="line998"></a> *
<a name="line999"></a> * @param {number} year 2 digit year value before adjustment.
<a name="line1000"></a> * @return {number} disambiguated year.
<a name="line1001"></a> * @private
<a name="line1002"></a> */
<a name="line1003"></a>goog.i18n.DateTimeParse.MyDate_.prototype.setTwoDigitYear_ = function(year) {
<a name="line1004"></a>  var now = new Date();
<a name="line1005"></a>  var defaultCenturyStartYear =
<a name="line1006"></a>      now.getFullYear() - goog.i18n.DateTimeParse.ambiguousYearCenturyStart;
<a name="line1007"></a>  var ambiguousTwoDigitYear = defaultCenturyStartYear % 100;
<a name="line1008"></a>  this.ambiguousYear = (year == ambiguousTwoDigitYear);
<a name="line1009"></a>  year += Math.floor(defaultCenturyStartYear / 100) * 100 +
<a name="line1010"></a>      (year &lt; ambiguousTwoDigitYear ? 100 : 0);
<a name="line1011"></a>  return this.year = year;
<a name="line1012"></a>};
<a name="line1013"></a>
<a name="line1014"></a>
<a name="line1015"></a>/**
<a name="line1016"></a> * Based on the fields set, fill a Date object. For those fields that not
<a name="line1017"></a> * set, use the passed in date object&#39;s value.
<a name="line1018"></a> *
<a name="line1019"></a> * @param {goog.date.DateLike} date Date object to be filled.
<a name="line1020"></a> * @param {boolean} validation If true, input string will be checked to make
<a name="line1021"></a> *     sure it is valid.
<a name="line1022"></a> *
<a name="line1023"></a> * @return {boolean} false if fields specify a invalid date.
<a name="line1024"></a> * @private
<a name="line1025"></a> */
<a name="line1026"></a>goog.i18n.DateTimeParse.MyDate_.prototype.calcDate_ =
<a name="line1027"></a>    function(date, validation) {
<a name="line1028"></a>  // year 0 is 1 BC, and so on.
<a name="line1029"></a>  if (this.era != undefined &amp;&amp; this.year != undefined &amp;&amp;
<a name="line1030"></a>      this.era == 0 &amp;&amp; this.year &gt; 0) {
<a name="line1031"></a>    this.year = -(this.year - 1);
<a name="line1032"></a>  }
<a name="line1033"></a>
<a name="line1034"></a>  if (this.year != undefined) {
<a name="line1035"></a>    date.setFullYear(this.year);
<a name="line1036"></a>  }
<a name="line1037"></a>
<a name="line1038"></a>  // The setMonth and setDate logic is a little tricky. We need to make sure
<a name="line1039"></a>  // day of month is smaller enough so that it won&#39;t cause a month switch when
<a name="line1040"></a>  // setting month. For example, if data in date is Nov 30, when month is set
<a name="line1041"></a>  // to Feb, because there is no Feb 30, JS adjust it to Mar 2. So Feb 12 will
<a name="line1042"></a>  // become  Mar 12.
<a name="line1043"></a>  var orgDate = date.getDate();
<a name="line1044"></a>
<a name="line1045"></a>  // Every month has a 1st day, this can actually be anything less than 29.
<a name="line1046"></a>  date.setDate(1);
<a name="line1047"></a>
<a name="line1048"></a>  if (this.month != undefined) {
<a name="line1049"></a>    date.setMonth(this.month);
<a name="line1050"></a>  }
<a name="line1051"></a>
<a name="line1052"></a>  if (this.day != undefined) {
<a name="line1053"></a>    date.setDate(this.day);
<a name="line1054"></a>  } else {
<a name="line1055"></a>    var maxDate =
<a name="line1056"></a>        goog.date.getNumberOfDaysInMonth(date.getFullYear(), date.getMonth());
<a name="line1057"></a>    date.setDate(orgDate &gt; maxDate ? maxDate : orgDate);
<a name="line1058"></a>  }
<a name="line1059"></a>
<a name="line1060"></a>  if (goog.isFunction(date.setHours)) {
<a name="line1061"></a>    if (this.hours == undefined) {
<a name="line1062"></a>      this.hours = date.getHours();
<a name="line1063"></a>    }
<a name="line1064"></a>    // adjust ampm
<a name="line1065"></a>    if (this.ampm != undefined &amp;&amp; this.ampm &gt; 0 &amp;&amp; this.hours &lt; 12) {
<a name="line1066"></a>      this.hours += 12;
<a name="line1067"></a>    }
<a name="line1068"></a>    date.setHours(this.hours);
<a name="line1069"></a>  }
<a name="line1070"></a>
<a name="line1071"></a>  if (goog.isFunction(date.setMinutes) &amp;&amp; this.minutes != undefined) {
<a name="line1072"></a>    date.setMinutes(this.minutes);
<a name="line1073"></a>  }
<a name="line1074"></a>
<a name="line1075"></a>  if (goog.isFunction(date.setSeconds) &amp;&amp; this.seconds != undefined) {
<a name="line1076"></a>    date.setSeconds(this.seconds);
<a name="line1077"></a>  }
<a name="line1078"></a>
<a name="line1079"></a>  if (goog.isFunction(date.setMilliseconds) &amp;&amp;
<a name="line1080"></a>      this.milliseconds != undefined) {
<a name="line1081"></a>    date.setMilliseconds(this.milliseconds);
<a name="line1082"></a>  }
<a name="line1083"></a>
<a name="line1084"></a>  // If validation is needed, verify that the uncalculated date fields
<a name="line1085"></a>  // match the calculated date fields.  We do this before we set the
<a name="line1086"></a>  // timezone offset, which will skew all of the dates.
<a name="line1087"></a>  //
<a name="line1088"></a>  // Don&#39;t need to check the day of week as it is guaranteed to be
<a name="line1089"></a>  // correct or return false below.
<a name="line1090"></a>  if (validation &amp;&amp;
<a name="line1091"></a>      (this.year != undefined &amp;&amp; this.year != date.getFullYear() ||
<a name="line1092"></a>       this.month != undefined &amp;&amp; this.month != date.getMonth() ||
<a name="line1093"></a>       this.day != undefined &amp;&amp; this.day != date.getDate() ||
<a name="line1094"></a>       this.hours &gt;= 24 || this.minutes &gt;= 60 || this.seconds &gt;= 60 ||
<a name="line1095"></a>       this.milliseconds &gt;= 1000)) {
<a name="line1096"></a>    return false;
<a name="line1097"></a>  }
<a name="line1098"></a>
<a name="line1099"></a>  // adjust time zone
<a name="line1100"></a>  if (this.tzOffset != undefined) {
<a name="line1101"></a>    var offset = date.getTimezoneOffset();
<a name="line1102"></a>    date.setTime(date.getTime() + (this.tzOffset - offset) * 60 * 1000);
<a name="line1103"></a>  }
<a name="line1104"></a>
<a name="line1105"></a>  // resolve ambiguous year if needed
<a name="line1106"></a>  if (this.ambiguousYear) { // the two-digit year == the default start year
<a name="line1107"></a>    var defaultCenturyStart = new Date();
<a name="line1108"></a>    defaultCenturyStart.setFullYear(
<a name="line1109"></a>        defaultCenturyStart.getFullYear() -
<a name="line1110"></a>        goog.i18n.DateTimeParse.ambiguousYearCenturyStart);
<a name="line1111"></a>    if (date.getTime() &lt; defaultCenturyStart.getTime()) {
<a name="line1112"></a>      date.setFullYear(defaultCenturyStart.getFullYear() + 100);
<a name="line1113"></a>    }
<a name="line1114"></a>  }
<a name="line1115"></a>
<a name="line1116"></a>  // dayOfWeek, validation only
<a name="line1117"></a>  if (this.dayOfWeek != undefined) {
<a name="line1118"></a>    if (this.day == undefined) {
<a name="line1119"></a>      // adjust to the nearest day of the week
<a name="line1120"></a>      var adjustment = (7 + this.dayOfWeek - date.getDay()) % 7;
<a name="line1121"></a>      if (adjustment &gt; 3) {
<a name="line1122"></a>        adjustment -= 7;
<a name="line1123"></a>      }
<a name="line1124"></a>      var orgMonth = date.getMonth();
<a name="line1125"></a>      date.setDate(date.getDate() + adjustment);
<a name="line1126"></a>
<a name="line1127"></a>      // don&#39;t let it switch month
<a name="line1128"></a>      if (date.getMonth() != orgMonth) {
<a name="line1129"></a>        date.setDate(date.getDate() + (adjustment &gt; 0 ? -7 : 7));
<a name="line1130"></a>      }
<a name="line1131"></a>    } else if (this.dayOfWeek != date.getDay()) {
<a name="line1132"></a>      return false;
<a name="line1133"></a>    }
<a name="line1134"></a>  }
<a name="line1135"></a>  return true;
<a name="line1136"></a>};
</pre>


</body>
</html>
